package com.hspedu.newData.dmsxl.树.二叉搜索树的操作;

import com.hspedu.newData.dmsxl.ds.TreeNode;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-19 
 * @Version: 1.0
 */

// todo
public class LC450删除二叉树搜索树中的节点 {

    public TreeNode deleteNode(TreeNode root, int key) {
        if (root==null) return null;
        if (root.val == key){
            if (root.left==null){
                return root.right;
            } else if (root.right==null) {
                return root.left;
            }else {
                TreeNode cur = root.right;

                while (cur.left!=null){
                    cur = cur.left;
                }
                cur.left = root.left;
                return root.right;
            }
        }

        if (root.val>key) root.left = deleteNode(root.left,key);
        if (root.val<key) root.right = deleteNode(root.right,key);
        return root;
    }

}
